{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"tSpeakUrl\":\"http://openapi.youdao.com/ttsapi?q=Mango+cake&langType=en&sign=B2DCB4D163D94EEE1CFE711D256D0788&salt=1594719377617&voice=4&format=mp3&appKey=44f107054ca586cd\",\"RequestId\":\"76ff50bc-332e-4634-bcea-99fc7bb74318\",\"web\":[{\"value\":[\"Mango mousse\",\"Mango Cake\"],\"key\":\"\\xe8\\x8a\\x92\\xe6\\x9e\\x9c\\xe8\\x9b\\x8b\\xe7\\xb3\\x95\"},{\"value\":[\"Pineapple Mango Cupcake\"],\"key\":\"\\xe8\\x8f\\xa0\\xe8\\x90\\x9d\\xe8\\x8a\\x92\\xe6\\x9e\\x9c\\xe6\\x9d\\xaf\\xe5\\xad\\x90\\xe8\\x9b\\x8b\\xe7\\xb3\\x95\"},{\"value\":[\"Mango Mousse Cake\"],\"key\":\"\\xe8\\x8a\\x92\\xe6\\x9e\\x9c\\xe6\\x85\\x95\\xe4\\xb8\\x9d\\xe8\\x9b\\x8b\\xe7\\xb3\\x95\"}],\"query\":\"\\xe8\\x8a\\x92\\xe6\\x9e\\x9c\\xe8\\x9b\\x8b\\xe7\\xb3\\x95\",\"translation\":[\"Mango cake\"],\"errorCode\":\"0\",\"dict\":{\"url\":\"yddict://m.youdao.com/dict?le=eng&q=%E8%8A%92%E6%9E%9C%E8%9B%8B%E7%B3%95\"},\"webdict\":{\"url\":\"http://m.youdao.com/dict?le=eng&q=%E8%8A%92%E6%9E%9C%E8%9B%8B%E7%B3%95\"},\"l\":\"zh-CHS2en\",\"isWord\":false,\"speakUrl\":\"http://openapi.youdao.com/ttsapi?q=%E8%8A%92%E6%9E%9C%E8%9B%8B%E7%B3%95&langType=zh-CHS&sign=6981E748AE8257E571FE7836C71EEB74&salt=1594719377617&voice=4&format=mp3&appKey=44f107054ca586cd\"}'\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import uuid\n",
    "import requests\n",
    "import hashlib\n",
    "import time\n",
    "from imp import reload\n",
    "\n",
    "import time\n",
    "\n",
    "reload(sys)\n",
    "\n",
    "YOUDAO_URL = 'https://openapi.youdao.com/api'\n",
    "APP_KEY = '44f107054ca586cd'\n",
    "APP_SECRET = 'NIGWY87ftsgE7FcqjsGUslAL3KgundLb'\n",
    "\n",
    "\n",
    "def encrypt(signStr):\n",
    "    hash_algorithm = hashlib.sha256()\n",
    "    hash_algorithm.update(signStr.encode('utf-8'))\n",
    "    return hash_algorithm.hexdigest()\n",
    "\n",
    "\n",
    "def truncate(q):\n",
    "    if q is None:\n",
    "        return None\n",
    "    size = len(q)\n",
    "    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]\n",
    "\n",
    "\n",
    "def do_request(data):\n",
    "    headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
    "    return requests.post(YOUDAO_URL, data=data, headers=headers)\n",
    "\n",
    "\n",
    "def connect():\n",
    "    q = \"芒果蛋糕\"\n",
    "\n",
    "    data = {}\n",
    "    data['from'] = '源语言'\n",
    "    data['to'] = '目标语言'\n",
    "    data['signType'] = 'v3'\n",
    "    curtime = str(int(time.time()))\n",
    "    data['curtime'] = curtime\n",
    "    salt = str(uuid.uuid1())\n",
    "    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET\n",
    "    sign = encrypt(signStr)\n",
    "    data['appKey'] = APP_KEY\n",
    "    data['q'] = q\n",
    "    data['salt'] = salt\n",
    "    data['sign'] = sign\n",
    "\n",
    "    response = do_request(data)\n",
    "    contentType = response.headers['Content-Type']\n",
    "    if contentType == \"audio/mp3\":\n",
    "        millis = int(round(time.time() * 1000))\n",
    "        filePath = \"合成的音频存储路径\" + str(millis) + \".mp3\"\n",
    "        fo = open(filePath, 'wb')\n",
    "        fo.write(response.content)\n",
    "        fo.close()\n",
    "    else:\n",
    "        print(response.content)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    connect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
